package org.lys.dao.impl.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.lys.dao.UserDao;
import org.lys.model.User;
import org.lys.util.Pagination;

public class UserDaoImpl extends BaseDaoImpl implements UserDao{

	
	/* (non-Javadoc)   
	 * @see org.lys.dao.impl.jdbc.UserDao#add(org.lys.model.User)   
	 */
	public int add(User user) {
		String sql = "insert into t_user(name) values (?) ";
		Object[] params = new Object[] { user.getName() };
		return Integer.parseInt(super.addRtnPk(sql, params).toString());
	}

	
	/* (non-Javadoc)   
	 * @see org.lys.dao.impl.jdbc.UserDao#delete(org.lys.model.User)   
	 */
	public void delete(User user) {
		if(user==null){
			return;
		}
		String sql = "delete from t_user where id=?";
		Object[] params = new Object[] { user.getId() };
		super.delete(sql, params);
	}

	
	/* (non-Javadoc)   
	 * @see org.lys.dao.impl.jdbc.UserDao#update(org.lys.model.User)   
	 */
	public void update(User user) {
		if(user==null){
			return;
		}
		String sql = "update t_user set name=? where id=? ";
		Object[] params = new Object[] { user.getName(),user.getId() };
		super.update(sql, params);
	}

	/*
	 * (non-Javadoc)   
	 * @see org.lys.dao.UserDao#query(org.lys.model.User)
	 */
	@Override
	public List<User> query(User user) {
		String sql = "select id, name  from t_user where name like ?  ";
		int arraySize = 1;
		int id = user.getId();
		if(id!=0){
			sql+="and id = ?";
			arraySize++;
		}
		String name = user.getName()==null?"":user.getName();
		Object[] args = new Object[arraySize];
		args[0]="%"+name+"%" ;
		
		if(id!=0){
			args[1]=id ;
		}
		List<User> list = super.find(sql, args,new RowMapper() {
			@Override
			public Object rowMapper(ResultSet rs) throws SQLException {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				return user;
			}
		});
		return list;
	}

	@Override
	public User get(int id) {
		String sql = "select id, name  from t_user where id = ?  ";
		Object[] args = new Object[]{id};
		return super.findForObject(sql, args,new RowMapper() {
			@Override
			public Object rowMapper(ResultSet rs) throws SQLException {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				return user;
			}
		});
	}


	@Override
	public int count(User user) {
		String sql = "select count(id) count  from t_user where name like ?  ";
		int arraySize = 1;
		int id = user.getId();
		if(id!=0){
			sql+="and id = ?";
			arraySize++;
		}
		String name = user.getName()==null?"":user.getName();
		Object[] params = new Object[arraySize];
		params[0]="%"+name+"%" ;
		
		if(id!=0){
			params[1]=id ;
		}
		return super.findForObject(sql, params, new RowMapper() {
			
			@Override
			public Object rowMapper(ResultSet rs) throws SQLException {
				return rs.getInt("count");
			}
		});
	}


	@Override
	public List<User> queryPage(User user, Pagination pagination) {
		String sql = "select id, name  from t_user where name like ?  ";
		int arraySize = 1;
		int id = user.getId();
		if(id!=0){
			sql+="and id = ?";
			arraySize++;
		}
		
		sql+=" limit ?,?";
		String name = user.getName()==null?"":user.getName();
		Object[] args = new Object[arraySize+2];
		args[0]="%"+name+"%" ;
		
		if(id!=0){
			args[1]=id ;
		}
		args[args.length-2] = pagination.getBeginRow();
		args[args.length-1] = pagination.getPageSize();
		
		List<User> list = super.find(sql, args,new RowMapper() {
			@Override
			public Object rowMapper(ResultSet rs) throws SQLException {
				User user = new User();
				user.setId(rs.getInt("id"));
				user.setName(rs.getString("name"));
				return user;
			}
		});
		return list;
	}
	
}
